package main

import (
	"database/sql"
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"time"
)

func main() {
	// MySQL 配置信息
	username := "root"  // 账号
	password := "root"  // 密码
	host := "127.0.0.1" // 地址
	port := 3306        // 端口
	DBname := "demo"    // 数据库名称
	timeout := "10s"    // 连接超时，10秒
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8&parseTime=True&loc=Local&timeout=%s", username, password, host, port, DBname, timeout)
	// Open 连接
	db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic("failed to connect mysql.")
	}
	fmt.Println(db)

	type Goods struct {
		ID        uint         `gorm:"column:id;primarykey"`
		Name      string       `gorm:"column:name"`
		Price     int          `gorm:"column:price"`
		CreatedAt time.Time    `gorm:"column:created_at"`
		UpdatedAt time.Time    `gorm:"column:updated_at"`
		DeletedAt sql.NullTime `gorm:"column:deleted_at"`
	}

	err = db.AutoMigrate(&Goods{})
	if err != nil {
		fmt.Println(err.Error())
	}

}
